package top.kaoshanji.p1ch01a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.*;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 代码清单-p1ch01a-12页a：GenericServletDemoServlet类
 * URL访问地址：/generic
 * author kaoshanji
 * ^_^
 * create time 2025/4/7 10:06
 */
@WebServlet(name = "GenericServletDemoServlet", urlPatterns = {"/generic"}, initParams = {
        @WebInitParam(name = "admin", value = "kao1"),
        @WebInitParam(name = "email", value = "admin1@xxx.com")
})
public class GenericServletDemoServlet extends GenericServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

        // 设置响应对象的编码和内容格式
        res.setContentType(ContentType.TEXT_HTML.toString());
        PrintWriter out = res.getWriter();

        ServletConfig servletConfig = getServletConfig();
        String admin = servletConfig.getInitParameter("admin");
        String email = servletConfig.getInitParameter("email");

        out.print("<html><head></head><body>" +
                "Admin:" + admin +
                "<br/>Email:" + email +
                "</body></html>");

    }


}

